﻿// <copyright file="MappedPathWindow.xaml.cs" company="Soumya Chattopadhyay">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <disclaimer> This software is intented for educational purposes only and not for 
// commerical or prodution use. The software is provided "as is", without warranty 
// of any kind, express or implied, including but not limited to the warranties of 
// merchantability, fitness for a particular purpose and noninfringement. In no event 
// shall the author(s) or copyright holder(s) be liable for any claim, damages or other 
// liability, whether in an action of contract, tort or otherwise, arising from, out of 
// or in connection with the software or the use or other dealings in the software.
// </disclaimer>

namespace My.CodePlex.TFSExplorer.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.TeamFoundation.VersionControl.Client;

    /// <summary>
    /// Interaction logic for MappedPathWindow.xaml
    /// </summary>
    public partial class MappedPathWindow : Window
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the MappedPathWindow class
        /// </summary>
        /// <param name="workspaces">The workspaces to display</param>
        public MappedPathWindow(WorkspaceInfo[] workspaces)
        {
            InitializeComponent();

            // Init the workspaces
            foreach (var workspace in workspaces)
            {
                ComboBoxItem comboBoxItem = new ComboBoxItem();
                comboBoxItem.Content = workspace.ServerUri.Host;
                comboBoxItem.Tag = workspace;

                this.WorkspacesComboBox.Items.Add(comboBoxItem);
            }

            this.WorkspacesComboBox.SelectionChanged += this.WorkspacesComboBox_SelectionChanged;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the selected mapped path
        /// </summary>
        public string MappedPath { get; set; }

        #endregion

        #region Private methods

        /// <summary>
        /// Change the mapped paths combo box
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The event arguments</param>
        private void WorkspacesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.WorkspacesComboBox.SelectedItem != null)
            {
                this.MappedPathsComboBox.Items.Clear();

                ComboBoxItem comboBoxItem = this.WorkspacesComboBox.SelectedItem as ComboBoxItem;
                WorkspaceInfo workspace = comboBoxItem.Tag as WorkspaceInfo;

                foreach (var paths in workspace.MappedPaths)
                {
                    ComboBoxItem comboBoxMapItem = new ComboBoxItem();
                    comboBoxMapItem.Content = paths;

                    this.MappedPathsComboBox.Items.Add(comboBoxMapItem);
                }
            }
        }

        /// <summary>
        /// Get the mapped path from the combo box
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The event arguments</param>
        private void SelectClick(object sender, RoutedEventArgs e)
        {
            if (this.MappedPathsComboBox.SelectedItem != null)
            {
                ComboBoxItem comboBoxItem = this.MappedPathsComboBox.SelectedItem as ComboBoxItem;
                this.MappedPath = (string)comboBoxItem.Content;
                this.DialogResult = true;
            }
        }

        #endregion
    }
}
